<?php
class subscribe extends spController
{
	/*
	function collect_user()
	{
		if(!$_SESSION['visitor_id']) return false;
		if(!spAccess('r',  'collect_user_' . $_SESSION['visitor_id'])) {
			$m_collect_user = spClass('m_collect_user');
			$result = $m_collect_user->addcollect();
			spAccess('w',  'collect_user_' . $_SESSION['visitor_id'], $result, 604800);
		}
	}
	*/

	function subscribe_add()
	{
		if($_SESSION['visitor_id'] == 0) return false;
		$store_id = intval($_POST['store_id']);
		if($store_id < 0) return false;
		$m_store = spClass("m_store");
		$store_result = $m_store->find(array('store_id'=>$store_id), '', 'store_name');
		/*
		 *	当用户不存在于店铺表中 则对服务进行反查，并入库
		 */
		if(empty($store_result)) {
			$owner_nick = $_POST['owner_nick'];
			$m_taobao = spClass('m_taobao');
			$shopgetreq = new ShopGetRequest;
			$shopgetreq->setFields("title,sid,cid");
			$shopgetreq->setNick($owner_nick);
			$resp = $m_taobao->m_topsdk->execute($shopgetreq);
			if($resp->code === 560) {
				exit('-2');
			}
			$m_store = spClass("m_store");
			if(($_GET['store_id'] && $resp->shop->sid !=$_GET['store_id']) || empty($_GET['store_id']) ) {
				if($m_store->find(array('store_id'=>$resp->shop->sid))) {
					exit('-1');
				}
			}
			$tbshopconvert = new TaobaokeShopsConvertRequest;
			
			$tbshopconvert->setFields("click_url,shop_type");
			$tbshopconvert->setSids($resp->shop->sid);
			$tbshopconvert->setNick("半价汇");
			//$tbshopconvert->setPid('mm_29938918_0_0'); //总是说报错，去提BUG //2012-2-3  已解决，添加中间的数字即可
			
			$convert_resp = $m_taobao->m_topsdk->execute($tbshopconvert);

			$store_result = array('store_name'=>$resp->shop->title, 'store_id'=>$resp->shop->sid, 'nickname'=>$owner_nick, 'alimama_url'=>$convert_resp->taobaoke_shops->taobaoke_shop[0]->click_url, 'type'=>$convert_resp->taobaoke_shops->taobaoke_shop[0]->shop_type, 'cid'=>$resp->shop->cid);
			$m_store->create($store_result);
			if(empty($result)) exit;
		}
		$m_collect_user = spClass('m_collect_user');
		
		$arr_create = array('visitor_id'=>$_SESSION['visitor_id'], 'visitor_nick'=>$_SESSION['visitor_nick'], 'store_name'=>$store_result['store_name'], 'store_id'=>$store_id);
		$m_collect_user->create($arr_create);
		exit('1');
	}

	function undo()
	{
	}
}
?>